package com.m3958.logcollector.domain;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.m3958.logcollector.util.common.BasicAuthenticationHeader;
import com.m3958.logcollector.util.common.UuidUtil;

/**
 * @author jianglibo@gmail.com
 *         2015年8月12日
 *
 */

@Entity
@Table(name = "kp", uniqueConstraints = { @UniqueConstraint(columnNames = { "keyId", "keySecret" }) })
public class Keypair extends BaseEntity {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String keyId;

    private String keySecret;

    private long useTimes;

    @ManyToOne
    @NotNull
    @JsonIgnore
    private Person creator;

    @PrePersist
    public void createPair() {
        if (getKeyId() == null) {
            setKeyId(UuidUtil.uuidNoDashUpcase());
            setKeySecret(UuidUtil.uuidNoDash());
        }
    }

    public BasicAuthenticationHeader asBasicAuthenticationHeader() {
        return new BasicAuthenticationHeader(getKeyId(), getKeySecret());
    }

    public String getKeyId() {
        return keyId;
    }

    public void setKeyId(String keyId) {
        this.keyId = keyId;
    }

    public String getKeySecret() {
        return keySecret;
    }

    public void setKeySecret(String keySecret) {
        this.keySecret = keySecret;
    }

    @JsonIgnore
    public Person getCreator() {
        return creator;
    }

    public void setCreator(Person creator) {
        this.creator = creator;
    }

    public long getUseTimes() {
        return useTimes;
    }

    public void setUseTimes(long useTimes) {
        this.useTimes = useTimes;
    }
}
